package com.chuanjiabao.wisdom.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.ReasionInfoCallbackBean;
import com.chuanjiabao.wisdom.databinding.ActivityCreateSecretInputBodyNumBinding;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.SpUtils;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class CreateSecretInputBodyNumActivity extends BaseActivity {

    ActivityCreateSecretInputBodyNumBinding binding;
    String pwd;
    ReasionInfoCallbackBean.DataBean bean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_create_secret_input_body_num);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        bean = (ReasionInfoCallbackBean.DataBean) SpUtils.getBean("module_reasion");
        pwd = getIntent().getStringExtra("module_num");
        binding.tvModuleOk.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tvModuleOk) {
            String bodyNum = binding.etModuleSecretPwd.getText().toString().trim();
            if (TextUtils.isEmpty(bodyNum)) {
                ToastUtil.showToast("请输入身份证信息");
                return;
            }

            if (bodyNum.length() != 18) {
                ToastUtil.showToast("请输入18位身份证信息");
                return;
            }

            setSecretPwd(bodyNum, pwd);
        }
    }

    public void setSecretPwd(String idCard, String pwd) {
        HTTPAPI.getInstance().setSecretPwd(idCard, pwd, pwd, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(CreateSecretInputBodyNumActivity.this, s);
                } else {
                    bean.setSet_secret(1);
                    SpUtils.putBean("module_reasion", bean);
                    ToastUtil.showToast("设置成功");
                    finish();
                }
            }
        });
    }

}
